package com.zbkj.common.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.zbkj.common.exception.CommonException;
import com.zbkj.common.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @Auther:
 * @Date: 2024/9/11 18:58
 * @Description:
 */
@Slf4j
public class AliSmsUtil {

    //产品名称:云通信短信API产品,开发者无需替换
    private static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    private static final String domain = "dysmsapi.aliyuncs.com";

    private static IAcsClient acsClient;

    public static AliSmsProperties aliSmsProperties;

    public static void smsClientInit() {
        if(Objects.isNull(acsClient)){
            //可自助调整超时时间
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-beijing", aliSmsProperties.getAccessKeyId(), aliSmsProperties.getAccessKeySecret());
            DefaultProfile.addEndpoint( "cn-beijing", product, domain);
            acsClient = new DefaultAcsClient(profile);
        }
    }

    public static void sendCode(String phoneNumbers, Integer code) {
        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phoneNumbers);
        //必填:短信签名-可在短信控制台中找到
//        request.setSignName("阿里云短信测试");
        request.setSignName(aliSmsProperties.getSignName());
        //必填:短信模板-可在短信控制台中找到
//        request.setTemplateCode("SMS_154950909");
        request.setTemplateCode(aliSmsProperties.getVerificationTempcode());
//        测试专用签名签名名称:阿里云短信测试使用场景:发送测试短信
//        测试专用模版
//        模版名称:测试专用模板模版Code:SMS_154950909模版类型:验证码
//        模版内容:您正在使用阿里云短信测试服务，体验验证码是：${code}，如非本人操作，请忽略本短信！
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        request.setTemplateParam(jsonObject.toJSONString());
        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");
        String result = null;
        try {
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if(Objects.nonNull(sendSmsResponse)) {
                if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                    result = "发送成功";
                }else{
                    result = sendSmsResponse.getMessage();
                    throw new CommonException(sendSmsResponse.getMessage());
                }
            } else {
                result = "返回null";
                throw new CommonException("短信验证码发送失败");
            }
        } catch (CommonException ce) {
            throw ce;
        } catch (Exception e) {
            log.error("sendCode-发送短信验证码-phone:{}-请求异常:",phoneNumbers,e);
            result = "请求异常";
            throw new CommonException("短信验证码发送失败");
        } finally {
            log.info("sendCode-发送短信验证码-phone:{}-result:{}",phoneNumbers,result);
        }
    }

    public static void sendMsg(String phoneNumbers,String templateCode,JSONObject jsonObject) throws ClientException {
        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phoneNumbers);
        //必填:短信签名-可在短信控制台中找到
//        request.setSignName("阿里云短信测试");
        request.setSignName(aliSmsProperties.getSignName());
        request.setTemplateCode(templateCode); //正式 SMS_236850153
        request.setTemplateParam(jsonObject.toJSONString());
        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            System.out.println("发送成功");
        }else{
            throw new CommonException(sendSmsResponse.getMessage());
        }
    }
}
